package com.mike.bdb.util;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

import com.mike.bdb.dpl.MyDbEnv;
import com.mike.bdb.model.Item;
import com.mike.bdb.model.Map;
import com.mike.bdb.model.Mission;
import com.mike.bdb.model.Pet;
import com.mike.bdb.model.PlayerRole;

public class DataMaker {
	public static final String FILE_BASE = "src/com/mike/bdb/dpl/";
	
	public static final String FILE_ITEM = FILE_BASE + "Item.txt";
	public static final String FILE_MAP = FILE_BASE + "Map.txt";
	public static final String FILE_MISSION = FILE_BASE + "Mission.txt";
	public static final String FILE_PET = FILE_BASE + "Pet.txt";
	public static final String FILE_PLAYERROLE = FILE_BASE + "PlayerRole.txt";
	
	static String[] dataFiles = { FILE_ITEM, FILE_MAP, FILE_MISSION, FILE_PET, FILE_PLAYERROLE };
	
	static MyDbEnv mydbEnv = new MyDbEnv();
	
	private static DataMaker instance = new DataMaker();
	
	public static void main(String[] args) {
		appendAllDataFile();
	}

	private static void appendAllDataFile() {
		mydbEnv.setup(Folder.getNewFolder("dpl-db"), false);
		for (String dataFile : dataFiles) {
			File df = new File(dataFile);
			prepare(df);
			appendToFile(df);
		}
		storeAlldata();
		mydbEnv.close();
	}

	private static void storeAlldata() {
	}

	private DataMaker() {
	}
	
	public static DataMaker getInstance() {
		return instance;
	}
	
	private static void appendPlayerRole(File df) {
		try {
			PrintWriter pw = new PrintWriter(df);
			for (int i = 1; i <= 10000; i++) {
				PlayerRole p = new PlayerRole();
				p.setId(String.valueOf(i));
				p.setName("Mike" + i);
				p.setSex(i % 2 == 0 ? (byte) 1 : (byte) 0);
				p.setAge((short)((100 + i) & 50));
				p.setEmail("zhaoming_" + i + "@gmail.com");
				p.setQq(i + "342" + i + "5");
				p.setPhoneNumber(i + " 2222 " + i + " 432");
				p.setAddress("广州市 天河区 壬丰大厦" + i + "楼");
				mydbEnv.getEntityStore().getPrimaryIndex(String.class, PlayerRole.class).put(p);
//				pw.append("" + p.getId());
//				pw.append("#");
//				pw.append("" + p.getName());
//				pw.append("#");
//				pw.append("" + p.getSex());
//				pw.append("#");
//				pw.append("" + p.getAge());
//				pw.append("#");
//				pw.append("" + p.getEmail());
//				pw.append("#");
//				pw.append("" + p.getQq());
//				pw.append("#");
//				pw.append("" + p.getPhoneNumber());
//				pw.append("#");
//				pw.append("" + p.getAddress());
//				pw.append("#");
//				pw.append("\r\n");
			}
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static void appendPet(File df) {
		try {
			PrintWriter pw = new PrintWriter(df);
			for (int i = 1; i <= 10000; i++) {
				Pet p = new Pet();
				p.setId(String.valueOf(i));
				p.setName("宠物-" + i);
				p.setHostPlayer("Mike-" + 1);
				p.setType((byte) (i % 5));
				p.setBornTimeMillis(System.currentTimeMillis());
				mydbEnv.getEntityStore().getPrimaryIndex(String.class, Pet.class).put(p);
//				pw.append("" + p.getId());
//				pw.append("#");
//				pw.append("" + p.getName());
//				pw.append("#");
//				pw.append("" + p.getHostPlayer());
//				pw.append("#");
//				pw.append("" + p.getType());
//				pw.append("#");
//				pw.append("" + p.getBornTimeMillis());
//				pw.append("#");
//				pw.append("\r\n");
			}
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
			
	}

	private static void appendMission(File df) {
		try {
			PrintWriter pw = new PrintWriter(df);
			for (int i = 1; i <= 50000; i++) {
				Mission p = new Mission();
				p.setId(String.valueOf(i));
				p.setName("任务-" + i);
				p.setPrefixId(i % 100);
				p.setSuffixId(i % 200);
				p.setType((byte) (i % 4));
				p.setDetail("请完成任务" + i);
				mydbEnv.getEntityStore().getPrimaryIndex(String.class, Mission.class).put(p);
//				pw.append("" + p.getId());
//				pw.append("#");
//				pw.append("" + p.getName());
//				pw.append("#");
//				pw.append("" + p.getPrefixId());
//				pw.append("#");
//				pw.append("" + p.getSuffixId());
//				pw.append("#");
//				pw.append("" + p.getType());
//				pw.append("#");
//				pw.append("" + p.getDetail());
//				pw.append("#");
//				pw.append("\r\n");
			}
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static void appendMap(File df) {
		try {
			PrintWriter pw = new PrintWriter(df);
			for (int i = 1; i <= 1000; i++) {
				Map p = new Map();
				p.setId(String.valueOf(i));
				p.setName("地图-" + i);
				p.setWidth(i % 100);
				p.setHeight(i % 200);
				mydbEnv.getEntityStore().getPrimaryIndex(String.class, Map.class).put(p);
//				pw.append("" + p.getId());
//				pw.append("#");
//				pw.append("" + p.getName());
//				pw.append("#");
//				pw.append("" + p.getWidth());
//				pw.append("#");
//				pw.append("" + p.getHeight());
//				pw.append("#");
//				pw.append("\r\n");
			}
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static void appendItem(File df) {
		try {
			PrintWriter pw = new PrintWriter(df);
			for (int i = 1; i <= 90000; i++) {
				Item p = new Item(i, i);
				p.setId("" + i);
				mydbEnv.getEntityStore().getPrimaryIndex(String.class, Item.class).put(p);
//				pw.append("" + p.getId());
//				pw.append("#");
//				pw.append("" + p.getAmount());
//				pw.append("#");
//				pw.append("" + p.getArticleId());
//				pw.append("#");
//				pw.append("" + p.getPlayerId());
//				pw.append("#");
//				pw.append("" + p.getDescription());
//				pw.append("#");
//				pw.append("\r\n");
			}
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private static void appendToFile(File df) {
		if(df.getName().contains("Item")) {
			appendItem(df);
		} else if (df.getName().contains("Map")) {
			appendMap(df);
		} else if (df.getName().contains("Mission")) {
			appendMission(df);
		} else if (df.getName().contains("Pet")) {
			appendPet(df);
		} else if (df.getName().contains("PlayerRole")) {
			appendPlayerRole(df);
		}
	}

	private static void prepare(File dataFile) {
		if(dataFile.exists()) {
			dataFile.delete();
			prepare(dataFile);
		} else {
			try {
				dataFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
